import { createStore } from 'vuex'

// 创建一个新的 store 实例
export default createStore({
  state () {
    return {
      username: '',
      userId: 0,
      userHeadimageurl: '',
      token:'',
      isLogin : "1"
    }
  },
  mutations: {
    setUserInfo: (state, username,userId,userHeadimageurl) => {
        state.username = username
        state.userId = userId
        state.userHeadimageurl = userHeadimageurl
        state.isLogin = 1
            // 把登录的用户的名保存到localStorage中，防止页面刷新，导致vuex重新启动，用户名就成为初始值（初始值为空）的情况
        localStorage.setItem('username', username)
        localStorage.setItem('userId', userId)
        localStorage.setItem('userHeadimageurl', userHeadimageurl)
        localStorage.setItem('isLogin', "1")
    },
    removeUserInfo: (state) => {
        state.username = ''
        state.userId = 0
        state.userHeadimageurl = ''
        state.isLogin = 0
        localStorage.removeItem('username')
        localStorage.removeItem('userId')
        localStorage.removeItem('userHeadimageurl')
        localStorage.removeItem('isLogin')
    },
    setToken:(state,token) => {
        state.token = token
        localStorage.setItem('token',token)
    },
    removeToken:(state) =>{
        localStorage.removeItem('token')
        state.token = ''
    }
  }
})